<template>
	<a-card :bordered="false">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="6">
					<a-form-item label="命名者" name="namedByMan">
						<a-input v-model:value="searchFormState.namedByMan" placeholder="请输入命名者" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="中文名" name="chineseName">
						<a-input v-model:value="searchFormState.chineseName" placeholder="请输入中文名" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="科中文名" name="branchChineseName">
						<a-input v-model:value="searchFormState.branchChineseName" placeholder="请输入科中文名" />
					</a-form-item>
				</a-col>
				<a-col :span="6" v-show="advanced">
					<a-form-item label="科字科名" name="sectionName">
						<a-input v-model:value="searchFormState.sectionName" placeholder="请输入科字科名" />
					</a-form-item>
				</a-col>
				<a-col :span="6" v-show="advanced">
					<a-form-item label="最大体长（cm)" name="maxBodyLength">
						<a-input v-model:value="searchFormState.maxBodyLength" placeholder="请输入最大体长（cm)" />
					</a-form-item>
				</a-col>
				<a-col :span="6" v-show="advanced">
					<a-form-item label="世界分布" name="worldDistribution">
						<a-input v-model:value="searchFormState.worldDistribution" placeholder="请输入世界分布" />
					</a-form-item>
				</a-col>
				<a-col :span="6" v-show="advanced">
					<a-form-item label="栖息环境" name="habitat">
						<a-input v-model:value="searchFormState.habitat" placeholder="请输入栖息环境" />
					</a-form-item>
				</a-col>
				<a-col :span="6" v-show="advanced">
					<a-form-item label="同种异名" name="synonyms">
						<a-input v-model:value="searchFormState.synonyms" placeholder="请输入同种异名" />
					</a-form-item>
				</a-col>
				<a-col :span="6" v-show="advanced">
					<a-form-item label="参考文献" name="cankaowenxian">
						<a-input v-model:value="searchFormState.cankaowenxian" placeholder="请输入参考文献" />
					</a-form-item>
				</a-col>
				<a-col :span="6" v-show="advanced">
					<a-form-item label="典藏标本" name="collectedSpecimens">
						<a-input v-model:value="searchFormState.collectedSpecimens" placeholder="请输入典藏标本" />
					</a-form-item>
				</a-col>
				<a-col :span="6" v-show="advanced">
					<a-form-item label="英文俗名" name="englishCommonName">
						<a-input v-model:value="searchFormState.englishCommonName" placeholder="请输入英文俗名" />
					</a-form-item>
				</a-col>
				<a-col :span="6" v-show="advanced">
					<a-form-item label="形态特征" name="morphologicalCharacteristics">
						<a-input v-model:value="searchFormState.morphologicalCharacteristics" placeholder="请输入形态特征" />
					</a-form-item>
				</a-col>
				<a-col :span="6" v-show="advanced">
					<a-form-item label="栖所生态" name="habitatEcology">
						<a-input v-model:value="searchFormState.habitatEcology" placeholder="请输入栖所生态" />
					</a-form-item>
				</a-col>
				<a-col :span="6" v-show="advanced">
					<a-form-item label="地理分布" name="geographicalDistribution">
						<a-input v-model:value="searchFormState.geographicalDistribution" placeholder="请输入地理分布" />
					</a-form-item>
				</a-col>
				<a-col :span="6" v-show="advanced">
					<a-form-item label="渔业利用" name="fisheryUtilization">
						<a-input v-model:value="searchFormState.fisheryUtilization" placeholder="请输入渔业利用" />
					</a-form-item>
				</a-col>
				<a-col :span="6" v-show="advanced">
					<a-form-item label="栖息深度" name="habitatDepth">
						<a-input v-model:value="searchFormState.habitatDepth" placeholder="请输入栖息深度" />
					</a-form-item>
				</a-col>
				<a-col :span="6" v-show="advanced">
					<a-form-item label="其他中文名" name="otherChineseName">
						<a-input v-model:value="searchFormState.otherChineseName" placeholder="请输入其他中文名" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-button type="primary" @click="tableRef.refresh()">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
					<a @click="toggleAdvanced" style="margin-left: 8px">
						{{ advanced ? '收起' : '展开' }}
						<component :is="advanced ? 'up-outlined' : 'down-outlined'"/>
					</a>
				</a-col>
			</a-row>
		</a-form>
		<s-table
			ref="tableRef"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.scientificName"
			:tool-config="toolConfig"
			:row-selection="options.rowSelection"
		>
			<template #operator class="table-operator">
				<a-space>
					<a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('fishDataAdd')">
						<template #icon><plus-outlined /></template>
						新增
					</a-button>
					<xn-batch-delete
						v-if="hasPerm('fishDataBatchDelete')"
						:selectedRowKeys="selectedRowKeys"
						@batchDelete="deleteBatchFishData"
					/>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="formRef.onOpen(record)" v-if="hasPerm('fishDataEdit')">编辑</a>
						<a-divider type="vertical" v-if="hasPerm(['fishDataEdit', 'fishDataDelete'], 'and')" />
						<a-popconfirm title="确定要删除吗？" @confirm="deleteFishData(record)">
							<a-button type="link" danger size="small" v-if="hasPerm('fishDataDelete')">删除</a-button>
						</a-popconfirm>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="tableRef.refresh()" />
</template>

<script setup name="data">
	import { cloneDeep } from 'lodash-es'
	import Form from './form.vue'
	import fishDataApi from '@/api/biz/fishDataApi'
	const searchFormState = ref({})
	const searchFormRef = ref()
	const tableRef = ref()
	const formRef = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	// 查询区域显示更多控制
	const advanced = ref(false)
	const toggleAdvanced = () => {
		advanced.value = !advanced.value
	}
	const columns = [
		{
			title: '命名者',
			dataIndex: 'namedByMan'
		},
		{
			title: '中文名',
			dataIndex: 'chineseName'
		},
		{
			title: '科中文名',
			dataIndex: 'branchChineseName'
		},
		{
			title: '科字科名',
			dataIndex: 'sectionName'
		},
		{
			title: '最大体长（cm)',
			dataIndex: 'maxBodyLength'
		},
		{
			title: '世界分布',
			dataIndex: 'worldDistribution'
		},
		{
			title: '栖息环境',
			dataIndex: 'habitat'
		},
		{
			title: '同种异名',
			dataIndex: 'synonyms'
		},
		{
			title: '参考文献',
			dataIndex: 'cankaowenxian'
		},
		{
			title: '典藏标本',
			dataIndex: 'collectedSpecimens'
		},
		{
			title: '英文俗名',
			dataIndex: 'englishCommonName'
		},
		{
			title: '濒危状态(是否在IUCN濒危名单中）1：是  0：否',
			dataIndex: 'endangeredStatus'
		},
		{
			title: '形态特征',
			dataIndex: 'morphologicalCharacteristics'
		},
		{
			title: '栖所生态',
			dataIndex: 'habitatEcology'
		},
		{
			title: '地理分布',
			dataIndex: 'geographicalDistribution'
		},
		{
			title: '渔业利用',
			dataIndex: 'fisheryUtilization'
		},
		{
			title: '栖息深度',
			dataIndex: 'habitatDepth'
		},
		{
			title: '有毒鱼 （1：是 0：否）',
			dataIndex: 'toxicFish'
		},
		{
			title: '经济鱼 （1：是 0：否）',
			dataIndex: 'economicFish'
		},
		{
			title: '食用鱼（1：是 0：否）',
			dataIndex: 'edibleFish'
		},
		{
			title: '观赏鱼 （1：是 0：否）',
			dataIndex: 'ornamentalFish'
		},
		{
			title: '图片',
			dataIndex: 'image'
		},
		{
			title: '其他中文名',
			dataIndex: 'otherChineseName'
		},
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['fishDataEdit', 'fishDataDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: 150
		})
	}
	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: true,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	const loadData = (parameter) => {
		const searchFormParam = cloneDeep(searchFormState.value)
		return fishDataApi.fishDataPage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}
	// 删除
	const deleteFishData = (record) => {
		let params = [
			{
				scientificName: record.scientificName
			}
		]
		fishDataApi.fishDataDelete(params).then(() => {
			tableRef.value.refresh(true)
		})
	}
	// 批量删除
	const deleteBatchFishData = (params) => {
		fishDataApi.fishDataDelete(params).then(() => {
			tableRef.value.clearRefreshSelected()
		})
	}
</script>
